package com.adminLogin.app.controller;

import com.adminLogin.app.entity.Admin;
import com.adminLogin.app.service.AdminService;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Controller
public class AdminLoginController01 {

    @Autowired
    private AdminService adminService;

    protected Logger logger = LogManager.getLogger(AdminLoginController01.class);

    //    登录页面
    @ResponseBody
    @GetMapping("adminlogin")
    public String Home(){
        logger.info("转到后台管理-登录页");
        return "admin/loginPage";
    }

    //登录验证
    @ResponseBody
    @RequestMapping(value = "AdminLogin",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public String checkLogin(HttpSession session, String username, String password){
        System.out.println(username);
        System.out.println(password);
        Admin admin1 = adminService.AdminLogin(username,password);
        JSONObject jsonObject = new JSONObject();
        if (admin1 == null){
            jsonObject.put("success",false);
            return "0";
        }else{
            session.setAttribute("admin",admin1);
            jsonObject.put("success",true);
            return "1";
        }
    }

}
